<template>
    <div class="mod-order" style="margin: 10px">
        <!--查询面板-->
        <SearchPanel ref="searchPanel" :search-config="searchConfig" @startSearch="startSearch">
            <!-- <el-form-item>
                <el-button icon="el-icon-plus" type="primary" size="mini" @click="add">新增</el-button>
            </el-form-item> -->

            <!-- <el-form-item>
                <el-button type="danger" size="mini" @click="batchDel" :disabled="selectDataList.length == 0">批量删除</el-button>
            </el-form-item> -->
        </SearchPanel>

        <!-- 查询结果 -->
        <ResultListPanel
            tableRef="table"
            ref="multipleTable"
            :checkBoxEnable="true"
            :data="dataList ? dataList : []"
            :listConfig="resultColConfig"
            :listLoading="listLoading"
            @actionClick="actionClick"
            @paginationChange="paginationChange"
            :tagTextExchange="tagTextExchange"
            :clcikTextShowExchange="clcikTextShowExchange"
            :clickTextExchange="clickTextExchange"
            :actionLimitName="actionLimitName"
            @selectionChange="selectionChange"
        ></ResultListPanel>
    </div>
</template>

<script>
import { INDEX_SEARCH_CONFIG, INDEX_SEARCH_RESULT_ITEMS, JYSJ_INDEX_SEARCH_RESULT_ITEMS } from './config';
import { isPostpone, isType } from '../../utils/util';
import ResultListPanel from '../../components/ResultListPanel';
import SearchPanel from '../../components/SearchPanel';
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';

export default {
    components: {
        ResultListPanel,
        SearchPanel
    },
    data() {
        return {
            visible: false,
            selectDataList: [],
            // 搜索项配置
            searchConfig: INDEX_SEARCH_CONFIG,
            resultColConfig: [],
            listLoading: false, // 数据列表显示状态
            dataList: [], //  数据列表
            /* 分页查询数据 */
            pageSize: 10,
            pageNum: 1,
            totalPage: 0,
            /* 查询条件参数 */
            dataForm: {
                name: ''
            },
            type: 1
        };
    },

    mounted() {
        this.getList();
    },

    watch: {
        //- 解决 同一页面，数据不刷新问题

        // 监听相同路由下参数变化的时候，从而实现异步刷新
        $route(to, from) {
            this.getList();
            this.nickname = this.$route.query.nickname; //修改页面标题
        }
    },

    methods: {
        selectionChange(val) {
            this.selectDataList = val;
        },

        /***
         * 搜索面板触发 搜索事件
         * @param params
         */
        startSearch(params) {
            this.dataForm = params;
            this.pageNum = 1;
            this.$set(this.$refs.multipleTable['pageParams'], 'pageNum', 1);

            this.getList();
        },

        /***
         * 触发 列表 操作按钮 （ 编辑 和 删除 ）
         * @param data
         * @param item
         */
        actionClick([data, item]) {
            if (item.alias === 'edit') {
                switch (this.$route.path) {
                    case '/postLoanManger/index':
                        //贷后管理

                        this.$router.push({ path: '/postLoanManger/details', query: { itemData: data } });

                        break;

                    case '/businessDataManger/index':
                        //经营数据管理

                        this.$router.push({ path: '/businessDataManger/details', query: { itemData: data, type: 2 } });

                        break;
                }
            }
        },

        tagTextExchange([data, item]) {
            //是否延期
            if (item.alias == 'isPostpone') {
                return isPostpone(data.isPostpone);
            }

            //还款类型
            if (item.alias == 'type') {
                return isType(data.type);
            }
        },

        clcikTextShowExchange([data, item]) {
            //查看合同
            if (item.alias == 'contract') {
                if (data.contract != null && data.contract !== '') {
                    return '查看合同';
                } else {
                    return '';
                }
            }
        },

        clickTextExchange([data, item]) {
            //下载合同
            if (item.alias == 'contract') {
                if (data.contract != null && data.contract !== '') {
                    window.location.href =
                        window.baseUrl + '/sys/file/new/down?TOKEN=' + sessionStorage.getItem('token') + '&id=' + data.contract;
                }
            }
        },
        actionLimitName([data, action]) {
            if (action.alias == 'edit') {
                switch (this.$route.path) {
                    case '/postLoanManger/index':
                        //贷后管理

                        return '编辑';
                        break;

                    case '/businessDataManger/index':
                        //经营数据管理

                        return '查看';
                        break;
                }
            }
        },

        /**
         * 分页器发生改变 触发事件
         * @param page
         */
        paginationChange(page) {
            this.pageSize = page.pageSize;
            this.pageNum = page.pageNo;
            this.getList();
        },

        // 列表 接口
        getList() {
            switch (this.$route.path) {
                case '/postLoanManger/index':
                    //贷后管理

                      this.resultColConfig = INDEX_SEARCH_RESULT_ITEMS;

                    break;

                case '/businessDataManger/index':
                    //经营数据管理

                   this.resultColConfig = JYSJ_INDEX_SEARCH_RESULT_ITEMS;

                    break;
            }

            let params = cloneDeep(this.dataForm);
            params.pageNum = this.pageNum;
            params.pageSize = this.pageSize;

            this.listLoading = true;

            API.postLoanManger.getList(params).then((res) => {
                console.log(res);

                this.listLoading = false;
                this.totalPage = res.result.total;
                this.dataList = res.result.rows;
                this.$set(this.$refs.multipleTable['pageParams'], 'total', res.result.total);
            });
        }
    }
};
</script>

<style>
</style>